﻿using RentHouse.DTO;
using RentHouse.IService;
using RentHouse.Service.Entitys;
using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace RentHouse.Service
{
    public class HousesService : IHousesService
    {
        DBModel _dbModel;

        public HousesService(DBModel _dbModel)
        {

            this._dbModel = _dbModel;

        }

        public int Create(HousesDTO housesDTO)
        {
            T_NewHouses newHouses = new T_NewHouses()
            {
                Name = housesDTO.Name,
                CommunityId = housesDTO.CommunityId,
                Address = housesDTO.Address,
                MonthRent = housesDTO.MonthRent,
                RoomTypeId = housesDTO.RoomTypeId,
                DecorateStatusId = housesDTO.DecorateStatusId,
                Area = housesDTO.Area
            };

            _dbModel.T_NewHouses.AddOrUpdate(newHouses);

            return  _dbModel.SaveChanges();

            
        }

        public string Delete(HousesDTO housesDTO)
        {
            throw new NotImplementedException();

        }

        public List<HousesDTO> GetList()
        {
            return _dbModel.T_NewHouses.Select(t => new HousesDTO
            {
                Id = t.Id,
                Name = t.Name,
                CommunityId = t.CommunityId,
                Address = t.Address,
                MonthRent = t.MonthRent.ToString(),
                RoomTypeId = t.RoomTypeId,
                DecorateStatusId = t.DecorateStatusId,
                Area = t.Area

            }).ToList();

            
        }

        /// <summary>
        /// 更新房源信息(图片)
        /// </summary>
        /// <param name="housesDTO"></param>
        /// <returns></returns>
        public string Update(int Id,string url)
        {
            T_NewHouses  newHouses = _dbModel.T_NewHouses.Where(t => t.Id == Id).FirstOrDefault();

            newHouses.Urlimg = url;

            _dbModel.T_NewHouses.AddOrUpdate(newHouses);

            int i =_dbModel.SaveChanges();

            if (i > 0)
            {
                return "success";
            }
            else
            {
                return "error";
            }
        
            
        }
    }
}
